import 'package:flutter/material.dart';
import 'package:flutter_app/utils/color.dart';

class LoginInput extends StatefulWidget {
  ///输入框自定义widget
  final String title;
  final String hint;
  final bool lineStretch;
  final bool obscureText;
  final bool enabled;
  final int maxLines;
  final ValueChanged<String> onChanged;
  final ValueChanged<bool> focuChanged;
  final TextInputType keyboardType;

  const LoginInput(this.title, this.hint,
      {Key key,
      this.lineStretch = false,
      this.obscureText = false,
      this.enabled = true,
      this.maxLines = 1,
      this.onChanged,
      this.focuChanged,
      this.keyboardType})
      : super(key: key);

  @override
  _LoginInputState createState() => _LoginInputState();
}

class _LoginInputState extends State<LoginInput> {
  final _focusNode = FocusNode();

  get maxLines => null;

  @override
  void initState() {
    super.initState();
    _focusNode.addListener(() {
      if (widget.focuChanged != null) {
        widget.focuChanged(_focusNode.hasFocus);
      }
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(children: [
          Container(
            padding: const EdgeInsets.only(left: 15),
            width: 100,
            child: Text(
              widget.title,
              style: const TextStyle(fontSize: 16),
            ),
          ),
          _input()
        ]),
        Padding(padding: EdgeInsets.symmetric(
            horizontal: !widget.lineStretch ? 20 : 0,
        ),
        child: const Divider(
          height: 1,
          thickness: 0.5,
        )
          )
      ],
    );
  }

  _input() {
    return Expanded(
        child: TextField(
      focusNode: _focusNode,
      onChanged: widget.onChanged,

      ///监听输入框内容
      obscureText: widget.obscureText,

      ///是否密码输入
      keyboardType: widget.keyboardType,

      ///键盘类型
      autocorrect: true,

      ///是否自动更正
      enabled: widget.enabled,

      ///是否可以输入。false不能，true能
      maxLines: widget.maxLines,

      ///这里是最大行数
      autofocus: !widget.obscureText,

      ///是否自动获取焦点
      cursorColor: primary,
      style: const TextStyle(
          fontSize: 16, color: Colors.black, fontWeight: FontWeight.w300),
      decoration: InputDecoration(
          contentPadding: const EdgeInsets.only(left: 20, right: 20),
          border: InputBorder.none,
          hintText: widget.hint ?? "",
          hintStyle: const TextStyle(fontSize: 15, color: Colors.grey)),
    ));
  }
}
